package com.xianniu.ltc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rebuild.core.UserContextHolder;
import com.xianniu.ltc.common.Constant;
import com.xianniu.ltc.mapper.TXnLimitRuleMapper;
import com.xianniu.ltc.model.entity.TUserDTO;
import com.xianniu.ltc.model.entity.TXnLimitRuleDTO;
import com.xianniu.ltc.model.po.AddXnLimitRulePO;
import com.xianniu.ltc.model.po.UpdateXnLimitRulePO;
import com.xianniu.ltc.model.vo.XnLimitRuleFallbackVO;
import com.xianniu.ltc.model.vo.XnLimitRuleVO;
import com.xianniu.ltc.service.XnLimitRuleService;
import com.xxl.sso.core.entity.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.beans.beancontext.BeanContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author WangRuiJie
 * @Title: XnLimitRuleServiceImpl
 * @Package com.xianniu.ltc.service.impl
 * @Description:
 * @date 2021/6/196:01 下午
 */
@Slf4j
@Service
public class XnLimitRuleServiceImpl implements XnLimitRuleService {

    @Resource
    private TXnLimitRuleMapper xnLimitRuleMapper;

    @Override
    public List<XnLimitRuleVO> selectCountRule(String entity) {
        List<XnLimitRuleVO> ruleDTOList = xnLimitRuleMapper.selectCountRule(entity);
        return ruleDTOList;
    }

    @Override
    public ReturnT isDisabledCountRule(String entity) {
        QueryWrapper<TXnLimitRuleDTO> wrapper = new QueryWrapper<>();
        wrapper.eq("entity", entity);
        wrapper.eq("rule_code", Constant.COUNT_RULE);
        List<TXnLimitRuleDTO> dtoList = xnLimitRuleMapper.selectList(wrapper);
        for (TXnLimitRuleDTO dto:dtoList) {
            if (Constant.INT_0.equals(dto.getIsDisable())){
                dto.setIsDisable(Constant.INT_1);
            }else {
                dto.setIsDisable(Constant.INT_0);
            }
            xnLimitRuleMapper.updateById(dto);
        }
        return new ReturnT(200, "操作成功");
    }

    @Override
    public ReturnT deleteCountRule(Integer id) {
        xnLimitRuleMapper.deleteById(id);
        return new ReturnT(200, "操作成功");
    }

    @Override
    public ReturnT addLeadCountRule(AddXnLimitRulePO po) {
        // 查询是否已存在
        QueryWrapper<TXnLimitRuleDTO> wrapper = new QueryWrapper<>();
        wrapper.eq("entity", po.getEntity().equals("XnLeadTeam")?Constant.LEAD_ENTITY:Constant.ACCOUNT_ENTITY);
        wrapper.eq("rule_code", Constant.COUNT_RULE);
        wrapper.eq("label", po.getLabel());
        TXnLimitRuleDTO queryDto = xnLimitRuleMapper.selectOne(wrapper);
        if (Objects.nonNull(queryDto)){
            queryDto.setLabel(po.getLabel());
            queryDto.setValue(po.getValue());
            xnLimitRuleMapper.updateById(queryDto);
        }else {
            TXnLimitRuleDTO dto = new TXnLimitRuleDTO();
            BeanUtils.copyProperties(po, dto);
            dto.setEntity(po.getEntity().equals("XnLeadTeam")?Constant.LEAD_ENTITY:Constant.ACCOUNT_ENTITY);
            dto.setRuleCode(Constant.COUNT_RULE);
            dto.setRuleName(Constant.COUNT_RULE_NAME);
            xnLimitRuleMapper.insert(dto);
        }
        return new ReturnT(200, "操作成功");
    }

    @Override
    public ReturnT updateRule(UpdateXnLimitRulePO po) {
        TXnLimitRuleDTO dto = new TXnLimitRuleDTO();
        BeanUtils.copyProperties(po, dto);
        xnLimitRuleMapper.updateById(dto);
        return new ReturnT(200, "操作成功");
    }

    @Override
    public ReturnT updateAgainRule(String entity, Integer v) {
        QueryWrapper<TXnLimitRuleDTO> wrapper = new QueryWrapper<>();
        wrapper.eq("entity", entity);
        wrapper.eq("rule_code", Constant.AGAIN_RULE);
        TXnLimitRuleDTO dto = xnLimitRuleMapper.selectOne(wrapper);
        dto.setValue(v);
        xnLimitRuleMapper.updateById(dto);
        return new ReturnT(200, "操作成功");
    }

    @Override
    public ReturnT updateAccountLockRule(Integer v) {
        QueryWrapper<TXnLimitRuleDTO> wrapper = new QueryWrapper<>();
        wrapper.eq("entity", Constant.ACCOUNT_ENTITY);
        wrapper.eq("rule_code", Constant.LOCK_RULE);
        TXnLimitRuleDTO dto = xnLimitRuleMapper.selectOne(wrapper);
        dto.setValue(v);
        xnLimitRuleMapper.updateById(dto);
        return new ReturnT(200, "操作成功");
    }

    @Override
    public XnLimitRuleVO selectReceiveRule(String entity) {
        XnLimitRuleVO vo = new XnLimitRuleVO();
        QueryWrapper<TXnLimitRuleDTO> wrapper = new QueryWrapper<>();
        wrapper.eq("entity", entity);
        wrapper.eq("rule_code", Constant.RECEIVE_RULE);
        TXnLimitRuleDTO dto = xnLimitRuleMapper.selectOne(wrapper);
        if (dto == null){
            dto = new TXnLimitRuleDTO();
            dto.setEntity(entity);
            dto.setRuleCode(Constant.RECEIVE_RULE);
            dto.setRuleName("领取规则");
            dto.setIsDisable(0);
            dto.setLabel("week");
            dto.setValue(0);
            dto.setTenantCode(UserContextHolder.getTenant());
            xnLimitRuleMapper.insert(dto);
        }
        BeanUtils.copyProperties(dto, vo);
        return vo;
    }

    @Override
    public XnLimitRuleFallbackVO selectFallbackRule(String entity) {
        XnLimitRuleFallbackVO vo = new XnLimitRuleFallbackVO();
        List<XnLimitRuleVO> fallbackVoList = new ArrayList<>();
        QueryWrapper<TXnLimitRuleDTO> wrapper = new QueryWrapper<>();
        wrapper.eq("entity", entity);
        wrapper.eq("rule_code", Constant.FALLBACK_RULE);
        List<TXnLimitRuleDTO> voList = xnLimitRuleMapper.selectList(wrapper);
        // 如果没有则初始化规则
        if (CollectionUtils.isEmpty(voList)){
            TXnLimitRuleDTO dto1 = new TXnLimitRuleDTO();
            dto1.setEntity(entity);
            dto1.setRuleCode(Constant.FALLBACK_RULE);
            dto1.setRuleName("回收规则");
            dto1.setIsDisable(0);
            dto1.setLabel("回收规则一");
            dto1.setValue(0);
            dto1.setTenantCode(UserContextHolder.getTenant());
            xnLimitRuleMapper.insert(dto1);
            TXnLimitRuleDTO dto2 = new TXnLimitRuleDTO();
            dto2.setEntity(entity);
            dto2.setRuleCode(Constant.FALLBACK_RULE);
            dto2.setRuleName("回收规则");
            dto2.setIsDisable(0);
            dto2.setLabel("回收规则二");
            dto2.setValue(0);
            dto2.setTenantCode(UserContextHolder.getTenant());
            xnLimitRuleMapper.insert(dto2);
            voList.add(dto1);
            voList.add(dto2);
            if (entity.equals(Constant.ACCOUNT_ENTITY)){
                TXnLimitRuleDTO dto3 = new TXnLimitRuleDTO();
                dto3.setEntity(entity);
                dto3.setRuleCode(Constant.FALLBACK_RULE);
                dto3.setRuleName("回收规则");
                dto3.setIsDisable(0);
                dto3.setLabel("回收规则三");
                dto3.setValue(0);
                dto3.setTenantCode(UserContextHolder.getTenant());
                xnLimitRuleMapper.insert(dto3);
                voList.add(dto3);
            }
        }
        for (TXnLimitRuleDTO dto:voList) {
            XnLimitRuleVO ruleVO = new XnLimitRuleVO();
            BeanUtils.copyProperties(dto, ruleVO);
            fallbackVoList.add(ruleVO);
        }
        vo.setFallbackVoList(fallbackVoList);
        vo.setIsDisable(fallbackVoList.get(0).getIsDisable());
        // 查询再次回收规则
        QueryWrapper<TXnLimitRuleDTO> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("entity", entity);
        wrapper1.eq("rule_code", Constant.AGAIN_RULE);
        List<XnLimitRuleVO> againRule = new ArrayList<>();
        TXnLimitRuleDTO againDto = xnLimitRuleMapper.selectOne(wrapper1);
        if (againDto == null){
            againDto = new TXnLimitRuleDTO();
            againDto.setEntity(entity);
            againDto.setRuleCode(Constant.AGAIN_RULE);
            againDto.setRuleName("再次领取规则");
            againDto.setIsDisable(0);
            againDto.setLabel("再次领取规则");
            againDto.setValue(0);
            againDto.setTenantCode(UserContextHolder.getTenant());
            xnLimitRuleMapper.insert(againDto);
        }
        XnLimitRuleVO againVo = new XnLimitRuleVO();
        BeanUtils.copyProperties(againDto, againVo);
        againRule.add(againVo);
        vo.setAgainRule(againRule);
        // 客户规则查询客户锁
        if (entity.equals(Constant.ACCOUNT_ENTITY)){
            QueryWrapper<TXnLimitRuleDTO> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("entity", entity);
            wrapper2.eq("rule_code", Constant.LOCK_RULE);
            TXnLimitRuleDTO lockDto = xnLimitRuleMapper.selectOne(wrapper2);
            if (lockDto == null){
                lockDto = new TXnLimitRuleDTO();
                lockDto.setEntity(entity);
                lockDto.setRuleCode(Constant.LOCK_RULE);
                lockDto.setRuleName("客户锁");
                lockDto.setIsDisable(0);
                lockDto.setLabel("account-lock");
                lockDto.setValue(0);
                lockDto.setTenantCode(UserContextHolder.getTenant());
                xnLimitRuleMapper.insert(lockDto);
            }
            vo.setLockRule(lockDto.getValue());
        }
        return vo;
    }

    @Override
    public ReturnT isDisabledFallback(String entity) {
        QueryWrapper<TXnLimitRuleDTO> wrapper = new QueryWrapper<>();
        wrapper.eq("entity", entity);
        wrapper.in("rule_code", Constant.FALLBACK_RULE,Constant.AGAIN_RULE);
        List<TXnLimitRuleDTO> dtoList = xnLimitRuleMapper.selectList(wrapper);
        for (TXnLimitRuleDTO dto:dtoList) {
            if (Constant.INT_0.equals(dto.getIsDisable())){
                dto.setIsDisable(Constant.INT_1);
            }else {
                dto.setIsDisable(Constant.INT_0);
            }
            xnLimitRuleMapper.updateById(dto);
        }
        return new ReturnT(200, "操作成功");
    }
}
